const mongoose = require('mongoose');
const { User } = require('./db/user');

async function testUserExists() {
  try {
    console.log('开始测试用户是否存在...\n');
    
    // 连接数据库
    await mongoose.connect('mongodb+srv://2334231668:xin123666@cluster0.13va5ah.mongodb.net/2410B');
    console.log('✅ 数据库连接成功');
    
    // 查找用户
    const userId = '68aeed055e85b6f7f7ff0480';
    console.log(`\n查找用户ID: ${userId}`);
    
    const user = await User.findById(userId);
    
    if (user) {
      console.log('✅ 用户存在:');
      console.log('- ID:', user._id);
      console.log('- 用户名:', user.username);
      console.log('- 头像:', user.avatar);
      console.log('- 创建时间:', user.createdAt);
    } else {
      console.log('❌ 用户不存在');
      
      // 查看所有用户
      console.log('\n查看所有用户:');
      const allUsers = await User.find({}).limit(5);
      allUsers.forEach(u => {
        console.log(`- ID: ${u._id}, 用户名: ${u.username}`);
      });
    }
    
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
  } finally {
    await mongoose.disconnect();
    console.log('\n数据库连接已关闭');
  }
}

// 运行测试
testUserExists();
